import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)
// 防止路由重复点击报错
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

const routes = [
  // 默认加载界面
  {
    path: '/',
    redirect: '/login'
  },
  // 登录页面界面
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login.vue')
  },
  // 注册页面路由
  {
    path: '/register',
    name: 'Register',
    component: () => import('@/views/Register.vue')
  },
  // 总路由
  {
    path: '/manager',
    name: 'manager',
    component: () => import('@/views/Manager.vue'),
    children:[
      // 个人护理
      {
        path: 'personalCare',
        component: () => import('@/views/PersonalCare/index.vue'),
      },
      // 预约
      {
        path: 'appointment',
        component: () => import('@/views/PersonalCare/appointment.vue'),
      },
      // 预约-医院详情
      {
        path: 'information',
        component: () => import('@/views/PersonalCare/information.vue'),
      },
      // 预约-科室
      {
        path: 'appointmentDetail',
        component: () => import('@/views/PersonalCare/appointmentDetail.vue'),
      },
      // 预约-排号
      {
        path: 'numDetail',
        component: () => import('@/views/PersonalCare/numDetail.vue'),
      },
      
      // 资讯
      {
        path: 'consult',
        component: () => import('@/views/Consult/consult.vue'),
      },
      // 资讯文章详情
      {
        name: 'Detail',
        path: 'article',
        component: () => import('@/views/Consult/detail.vue')
      },
      // 更多热搜
      {
        name: 'MoreSearch',
        path: 'moreSearch',
        component: () => import('@/views/Consult/more.vue')
      },
      // 资讯中的招募令
      {
        name: 'Part',
        path: 'part',
        component: () => import('@/views/Consult/part.vue')
      },

      // 设备
      {
        path: 'equipment',
        component: () => import('@/views/Equipment/equipment.vue')
      },
      // 设备详情
      {
        path: 'equimentDetail',
        component: () => import('@/views/Equipment/equipmentDetail.vue')
      },

      // 统计
      {
        path: 'count',
        component: () => import('@/views/Count/count.vue')
      },

      // 我的
      {
        path: 'mine',
        component: () => import('@/views/Mine/mine.vue')
      },
      {
        path: 'personal',
        component: () => import('@/views/Mine/personal.vue')
      },
      {
        path: 'assessment',
        component: () => import('@/views/Mine/assessment.vue')
      },
      {
        path: 'history',
        component: () => import('@/views/Mine/history.vue')
      },
      {
        path: 'monitoring',
        component: () => import('@/views/Mine/monitoring.vue')
      },
      // 地址列表
      {
        path: 'address',
        component: () => import('@/views/Mine/address.vue'),
      },
      // 产品页面
      {
        path: 'product',
        component: () => import('@/views/Product/product.vue'),
      },
      // 订单提交页面
      {
        path: 'orderConfirm',
        component: () => import('@/views/Product/orderConfirm.vue'),
      }
    ]
  },
  
]

const router = new VueRouter({
  // mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 路由守卫 判断有无token
// 使用 router.beforeEach 注册一个全局前置守卫，判断用户是否登陆
router.beforeEach((to, from, next) => {
  //若要跳转的页面是登录界面
  if (to.path === '/login') {
    //直接跳转
    next();
  } else { //若想要跳转其他页面
    //获取本地存储的token值
    let token = localStorage.getItem('token');
    //若token为空则验证不成功，跳转到登录页面
    if (token === 'null' || token === '') {
      next('/login');
    } else {
      next();
    }
  }
});

export default router
